package com.horsen.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.horsen.service.edu.mapper.ChapterMapper;
import com.horsen.service.edu.mapper.VideoMapper;
import com.horsen.service.edu.model.Chapter;
import com.horsen.service.edu.model.VO.ChapterVO;
import com.horsen.service.edu.model.VO.VideoVO;
import com.horsen.service.edu.model.Video;
import com.horsen.service.edu.service.ChapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Horsen
 * @since 2021-04-06
 */
@Slf4j
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoMapper videoMapper;

    /**
     * 根据ID删除章节
     *
     * @param id 章节ID
     * @return 删除是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(String id) {
        // 删除视频
        Boolean videoResult = this.deleteVideoById(id);
        if (videoResult) {
            log.info("删除视频成功");
        } else {
            log.info("删除视频失败");
        }
        LambdaQueryWrapper<Video> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Video::getChapterId, id);
        videoMapper.delete(queryWrapper);
        // 删除章节
        return this.removeById(id);
    }

    /**
     * 根据章节ID删除视频
     *
     * @param id 章节ID
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteVideoById(String id) {
        // TODO: 从VOD删除视频
        return false;
    }

    /**
     * 获取章节>视频树形列表
     *
     * @param courseId 课程ID
     * @return 树形列表
     */
    @Override
    public List<ChapterVO> getTreeList(String courseId) {

        List<ChapterVO> chapterVOList = new ArrayList<>();

        // 获取章节信息
        LambdaQueryWrapper<Chapter> chapterQueryWrapper = Wrappers.lambdaQuery();
        chapterQueryWrapper.eq(Chapter::getCourseId, courseId);
        chapterQueryWrapper.orderByDesc(Chapter::getSort);
        chapterQueryWrapper.orderByAsc(Chapter::getGmtCreate);
        List<Chapter> chapterList = baseMapper.selectList(chapterQueryWrapper);
        // 获取视频信息
        LambdaQueryWrapper<Video> videoQueryWrapper = Wrappers.lambdaQuery();
        videoQueryWrapper.eq(Video::getCourseId, courseId);
        videoQueryWrapper.orderByDesc(Video::getSort);
        videoQueryWrapper.orderByAsc(Video::getGmtCreate);
        List<Video> videoList = videoMapper.selectList(videoQueryWrapper);
        // 组装Chapter列表
        for (Chapter chapter : chapterList) {
            // 创建ChapterVO对象
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(chapter, chapterVO);
            // 组装Video列表
            List<VideoVO> videoVOList = new ArrayList<>();
            for (Video video : videoList) {
                if (chapter.getId().equals(video.getChapterId())) {
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(video, videoVO);
                    videoVOList.add(videoVO);
                }
            }
            chapterVO.setChildren(videoVOList);
            chapterVOList.add(chapterVO);
        }
        return chapterVOList;
    }
}
